﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 强制设置对应品质等级的组件或对象，跳过全局品质等级
    /// </summary>
    public class QualityOverrideControl : MonoBehaviour
    {
        [Serializable]
        public struct ControlItem
        {
            public UnityEngine.Object Target;
            public bool[] EnabledLevels;
        }

        [SerializeField]
        [Tooltip("The quality control items.")]
        private ControlItem[] controls;
        private LinkedListNode<Action> onQualityChangedNode; //品质切换回调事件
        private int overrideLevel = -1; //保存指定的品质等级

        void Awake()
        {
            onQualityChangedNode = QualityConfig.ListenQualityChanged(OnQualityLevelChanged);
            OnQualityLevelChanged();
        }


#if UNITY_EDITOR
        private void Reset()
        {
            if (this.controls == null || this.controls.Length == 0)
            {
                var item = new ControlItem();
                item.Target = this.gameObject;
                item.EnabledLevels = new bool[] { false, true, true };
                this.controls = new ControlItem[] { item };
            }
        }

        public void TestQualityLevel(int level)
        {
            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }

                if (ctrl.Target != null)
                {
                    var behaviour = ctrl.Target as Behaviour;
                    if (behaviour != null) behaviour.enabled = _enabled;
                    else
                    {
                        var go = ctrl.Target as GameObject;
                        if (go != null) go.SetActive(_enabled);
                    }
                }
            }
        }
#endif

        private void OnDestroy()
        {
            if (onQualityChangedNode != null)
            {
                QualityConfig.UnlistenQualtiy(onQualityChangedNode);
                onQualityChangedNode = null;
            }
        }

        /// <summary>
        /// 设置指定品质等级的事件
        /// </summary>
        /// <param name="level"></param>
        public void SetOverrideLevel(int level)
        {
            this.overrideLevel = level;
            this.OnQualityLevelChanged(level);
        }

        /// <summary>
        /// 重置事件，使用全局品质等级事件
        /// </summary>
        public void ResetOverrideLevel()
        {
            var level = QualityConfig.QualityLevel;
            if (this.overrideLevel != level)
            {
                this.OnQualityLevelChanged(level);
            }

            this.overrideLevel = -1;
        }

        private void OnQualityLevelChanged()
        {
            if (this.overrideLevel < 0)
            {
                var level = QualityConfig.QualityLevel;
                this.OnQualityLevelChanged(level);
            }
        }

        private void OnQualityLevelChanged(int level)
        {
            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }

                if (ctrl.Target != null)
                {
                    var behaviour = ctrl.Target as Behaviour;
                    if (behaviour != null) behaviour.enabled = _enabled;
                    else
                    {
                        var go = ctrl.Target as GameObject;
                        if (go != null) go.SetActive(_enabled);
                    }
                }
            }
        }

    }
}

